package com.flyou.ccnucenter.ui.activity.imageDetail;

import android.net.Uri;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.flyou.ccnucenter.Constant;
import com.flyou.ccnucenter.R;
import com.flyou.ccnucenter.photoview.PhotoView;
import com.flyou.ccnucenter.photoview.PhotoViewAttacher;
import com.flyou.ccnucenter.ui.activity.BaseActivity1;
import com.flyou.ccnucenter.utils.MyLog;
import com.flyou.ccnucenter.utils.OkHttpClientManager;
import com.flyou.ccnucenter.utils.SDCardUtils;
import com.flyou.ccnucenter.utils.ScreenUtils;
import com.squareup.okhttp.Request;

import java.io.File;

import static android.view.WindowManager.*;

public class ImageDetailActivity extends BaseActivity1 implements View.OnClickListener {
    private SimpleDraweeView iv_photos_view;
    private PhotoViewAttacher mAttacher;
    private RelativeLayout image_detial;
    private TextView downloadImage;
    private String henu_item_image;
    PhotoView photoView;
    private String henu_image_item_down_path;

    @Override
    protected void initView() {
        getWindow().setFlags(LayoutParams.FLAG_FULLSCREEN,
                LayoutParams.FLAG_FULLSCREEN);//设置全屏
        setContentView(R.layout.activity_image_detail);
        photoView = (PhotoView) findViewById(R.id.photo_view);
        downloadImage = (TextView) findViewById(R.id.tv_download_image);

    }

    @Override
    protected void initDate() {
        String henu_item_image_icon_url = getIntent().getStringExtra(Constant.HENU_ITEM_IMAGE_ICON_URL);
        henu_item_image = getIntent().getStringExtra(Constant.HENU_ITEM_IMAGE_URL);
        if (henu_item_image_icon_url.isEmpty()) {
            Toast.makeText(this, "图片已经被移除，或服务器问题", Toast.LENGTH_SHORT).show();
            finish();
        } else {


                String image_name = henu_item_image.substring(henu_item_image.length() - 21, henu_item_image.length());
                String localImagePath = SDCardUtils.getSDCardPath() + File.separator + "henu_image" ;
                File file = new File(localImagePath,image_name);
            MyLog.d(localImagePath);
                if (file.exists()) {
                    downloadImage.setVisibility(View.GONE);
//                    不再设置加载本地高清图片
                    photoView.setImageURI(Uri.parse("file://" + file.getAbsolutePath()));


                    photoView.setImageUri(henu_item_image, ScreenUtils.getScreenWidth(ImageDetailActivity.this), ScreenUtils.getScreenHeight(ImageDetailActivity.this));
                }
                else {
                    photoView.setImageUri(henu_item_image, ScreenUtils.getScreenWidth(ImageDetailActivity.this), ScreenUtils.getScreenHeight(ImageDetailActivity.this));
                }
            }

        }


    @Override
    protected void setListener() {
        downloadImage.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        downloadImage.setText("正在下载图片……");
        String imgaeFilePath = null;
        if (SDCardUtils.isSDCardEnable()) {
            File imgaeFile = new File(SDCardUtils.getSDCardPath() + File.separator + "henu_image");
            if (!imgaeFile.exists()) {
                imgaeFile.mkdirs();
            }
            imgaeFilePath = imgaeFile.getAbsolutePath();

            OkHttpClientManager.getDownloadDelegate().downloadAsyn(henu_item_image, imgaeFilePath, new OkHttpClientManager.ResultCallback<String>() {
                @Override
                public void onError(Request request, Exception e) {
                    Toast.makeText(ImageDetailActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onResponse(String response) {
                    downloadImage.setVisibility(View.GONE);
                    Toast.makeText(ImageDetailActivity.this, "下载成功，保存路径：" + response, Toast.LENGTH_SHORT).show();
                    henu_image_item_down_path = response;
                    MyLog.d(response);
                }

                @Override
                public void onAfter() {
                    super.onAfter();
//                    photoView.setImageURI(Uri.parse("file://" + henu_image_item_down_path));
                }
            });
        } else {
            Toast.makeText(this, "内存卡不存在，无法下载", Toast.LENGTH_SHORT).show();
        }
    }

    //判断文件是否存在
    public boolean fileIsExists(String strFile) {
        try {
            File f = new File(strFile);
            if (!f.exists()) {
                return false;
            }

        } catch (Exception e) {
            return false;
        }

        return true;
    }
}
